/**
  * Author: Mei
  * Date: 2020-10-15 14:07
  * Description: 
  *
  */
#include <iostream>
#include "CTime.h"

using namespace std;

//构造函数
CTime::CTime(int h = 0, int s = 0, int m = 0) {
    this->hour = h;
    this->second = s;
    this->minute = m;
}

//比较运算符
bool CTime::operator>(CTime &t) {
    if (this->hour > t.hour) {
        return true;
    } else if (this->hour == t.hour && this->minute > t.minute) {
        return true;
    } else if (this->hour == t.hour && this->minute == t.minute && this->second > t.second) {
        return true;
    } else {
        return false;
    }
}

bool CTime::operator<(CTime &t) {
    if (this->hour < t.hour) {
        return true;
    } else if (this->hour == t.hour && this->minute < t.minute) {
        return true;
    } else if (this->hour == t.hour && this->minute == t.minute && this->second < t.second) {
        return true;
    } else {
        return false;
    }
}

bool CTime::operator>=(CTime &t) {
    if (this->hour >= t.hour) {
        return true;
    } else if (this->hour == t.hour && this->minute >= t.minute) {
        return true;
    } else if (this->hour == t.hour && this->minute == t.minute && this->second >= t.second) {
        return true;
    } else {
        return false;
    }
}

bool CTime::operator<=(CTime &t) {
    if (this->hour <= t.hour) {
        return true;
    } else if (this->hour == t.hour && this->minute <= t.minute) {
        return true;
    } else if (this->hour == t.hour && this->minute == t.minute && this->second <= t.second) {
        return true;
    } else {
        return false;
    }
}

bool CTime::operator==(CTime &t) {
    if (this->hour == t.hour && this->minute == t.minute && this->second == t.second) {
        return true;
    } else {
        return false;
    }
}

bool CTime::operator!=(CTime &t) {
    if (this->hour != t.hour) {
        return true;
    } else if (this->hour == t.hour && this->minute != t.minute) {
        return true;
    } else if (this->hour == t.hour && this->minute == t.minute && this->second != t.second) {
        return true;
    } else {
        return false;
    }
}

//二目运算符的重载
CTime &CTime::operator+(CTime &t) {
    int h = this->hour + t.hour;
    int m = this->minute + t.minute;
    int s = this->second + t.second;
    if (s >= 60) {
        s -= 60;
        ++m;
    }
    if (m >= 60) {
        m -= 60;
        ++h;
    }
    if (h >= 24) {
        h -= 24;
    }
    Ctime time(h, m, s);
    return time;
}

CTime &CTime::operator-(CTime &t) { //9:20:00  8:40:00
    int h = this->hour - t.hour;
    int m = this->minute - t.minute;
    int s = this->second - t.second;
    if (h < 0 || (h == 0 && m < 0) || (h == 0 && m == 0 && s < 0)) {
        h = 0; //说明不能减
    } else {
        if (s < 0) {
            s += 60;
            --(this->minute);
        }
        if (m < 0) {
            m += 60;
            --(this->hour);
        }
    }
    Ctime time(h, m, s);
    return time;
}

CTime &CTime::operator+(int s) {
    int h = this->hour;
    int m = this->minute;
    int s = this->second + s;
    if (s >= 60) {
        s -= 60;
        ++m;
    }
    if (m >= 60) {
        m -= 60;
        ++h;
    }
    if (h >= 24) {
        h -= 24;
    }
    Ctime time(h, m, s);
    return time;

}

CTime &CTime::operator-(int s) { //00 00 20
    int h = this->hour;
    int m = this->minute;
    int s = this->second - s;
    int t = h * 60 * 60 + m * 60 + this->second - s;
    if (t < s) {
        h = 0; //说明不能减
    } else {
        if (t < 60) {
            s = t;
            h = 0;
            m = 0;
        } else if (t < 60 * 60) {
            h = 0;
            m = t / 60;
            s = t % 60;
        } else if (t < 60 * 60 * 24) { // 01:10:00 60*60+10*60=3600+600 4210%60
            h = t / (60 * 60);
            m = t % (60 * 60) / 60;
            s = t % 60;
        }
    }
    Ctime time(h, m, s);
    return time;

}

//一目运算符的重载
CTime &CTime::operator++(int) { //i++
    CTime t(*this); //创建++之前的对象
    int second = this->second;
    ++second;
    if (second > 60) {
        second -= 60;
        ++(this->minute);
    }
    if (this->minute > 60) {
        this->minute -= 60;
        ++this->hour;
    }
    if (this->hour > 24) {
        this->hour -= 24;
    }
    return t;
}

CTime &CTime::operator++() {
    int second = this->second;
    ++second;
    if (second > 60) {
        second -= 60;
        ++(this->minute);
    }
    if (this->minute > 60) {
        this->minute -= 60;
        ++this->hour;
    }
    if (this->hour > 24) {
        this->hour -= 24;
    }
    return *this;
}

CTime &CTime::operator--(int) { //i--
    CTime t(*this);
    int second = this->second;
    --second;
    if (second < 0) {
        --minute;
        second += 60;
    }
    if (this->minute > 0) {
        this->minute += 60;
        --this->hour;
    }
    return t;
}

CTime &CTime::operator--() {
    int second = this->second;
    --second;
    if (second < 0) {
        --minute;
        second += 60;
    }
    if (this->minute > 0) {
        this->minute += 60;
        --this->hour;
    }
    return *this;
}

//赋值运算符的重载
CTime &CTime::operator+=(CTime &t) {
    int s = this->second + t.second;
    if (s > 60) {
        s -= 60;
        ++this->minute;
    }
    int m = this->minute + t.minute;
    if (m > 60) {
        m -= 60;
        ++this->hour;
    }
    int h = this->hour + t.hour;
    if (h > 24) {
        h -= 24;
    }
    return *this;
}

CTime &CTime::operator-=(CTime &t) {
    int h = this->hour - t.hour;
    int m = this->minute - t.minute;
    int s = this->second - t.second;
    if (h < 0 || (h == 0 && m < 0) || (h == 0 && m == 0 && s < 0)) {
        h = 0; //说明不能减
    } else {
        if (s < 0) {
            s += 60;
            --(this->minute);
        }
        if (m < 0) {
            m += 60;
            --(this->hour);
        }
    }
    this->hour = h;
    this->minute = m;
    this->second = s;
    return *this;
}

CTime &CTime::operator+=(int s) {
    int t = this->second + s;
    if (t > 60) {
        t -= 60;
        ++this->minute;
    }
    if (m > 60) {
        m -= 60;
        ++this->hour;
    }
    if (h > 24) {
        h -= 24;
    }
    return *this;

}

CTime &CTime::operator-=(int t) {
    int h = this->hour;
    int m = this->minute;
    int s = this->second - t;
    if (h < 0 || (h == 0 && m < 0) || (h == 0 && m == 0 && s < 0)) {
        h = 0; //说明不能减
    } else {
        if (s < 0) {
            s += 60;
            --(this->minute);
        }
        if (m < 0) {
            m += 60;
            --(this->hour);
        }
    }
    this->hour = h;
    this->minute = m;
    this->second = s;
    return *this;
}

//输入和输出友元函数重载
istream &operator>>(istream &in, CTime &t) { //11:12:12
    cout << "请按照：hh:mm:ss的格式输入时间" << endl;
    char sign1, sign2;
    int h, m, s;
    do {
        in >> h >> sign1 >> m >> sign2 >> s;

    } while (!(sign1 == ':' && sign2 == ':'));
    t.hour = h;
    t.second = s;
    t.minute = m;

    return in;
}

//输出
ostream &operator<<(ostream &out, const CTime &t) {
    out << t.hour << ":" << t.minute << ":" << t.second << endl;
    return out;
}